﻿using Microsoft.IdentityModel.JsonWebTokens;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text;
using NetMicro.Core.ApiFunction;
using NetMicro.Core.Ioc.Dependencies;

namespace NetMicro.JwtBearer
{
    /// <summary>
    /// Jwttoken处理
    /// </summary>
    public interface IJwtHandler : IScoped
    {

        /// <summary>
        /// 生成 Token
        /// </summary>
        /// <param name="payload"></param>
        /// <param name="expiredTime">过期时间（分钟）</param>
        /// <returns></returns>
        string CreateToken(IDictionary<string, object> payload, long? expiredTime = null);


        /// <summary>
        /// 生成 Token
        /// </summary>
        /// <param name="issuerSigningKey"></param>
        /// <param name="payload"></param>
        /// <param name="algorithm"></param>
        /// <returns></returns>
        string CreateToken(string issuerSigningKey, string payload, string algorithm = SecurityAlgorithms.HmacSha256);


        /// <summary>
        /// 生成刷新 Token
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="expiredTime">刷新 Token 有效期（分钟）</param>
        /// <returns></returns>
        string CreateRefreshToken(string accessToken, int expiredTime = 43200);


        /// <summary>
        /// 通过过期Token 和 刷新Token 换取新的 Token
        /// </summary>
        /// <param name="expiredToken"></param>
        /// <param name="refreshToken"></param>
        /// <param name="expiredTime">过期时间（分钟）</param>
        /// <returns></returns>
        string RefreshToken(string expiredToken, string refreshToken, long? expiredTime = null);



        /// <summary>
        /// 验证 Token
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="validateLifetime"></param>
        /// <returns></returns>
        (bool IsValid, JsonWebToken Token, TokenValidationResult validationResult) Validate(string accessToken, bool validateLifetime = true);

        /// <summary>
        /// 读取 Token，不含验证
        /// </summary>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        JsonWebToken ReadJwtToken(string accessToken);
    }
}
